package com.example.springboot_myproject.Controller;

import com.example.springboot_myproject.Model.Student;
import com.example.springboot_myproject.Model.StudentFuzzyQuery;
import com.example.springboot_myproject.Service.StudentServie;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Resource
    private StudentServie studentService;

    //  发布各个页面
//    首页
    @RequestMapping("index")
    public String index() {
        return "index";
    }

    //  table栏
    @RequestMapping("table")
    public String index_table() {
        return "table";
    }

    @RequestMapping("addform")
    public String index_add() {
        return "addform";
    }

    @RequestMapping("editform")
    public String index_edit() {
        return "editform";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> queryStudentByParams(StudentFuzzyQuery studentFuzzyQuery,
                                                    String sname, String sno, String sjob) {
        return studentService.querystudentbyParams(studentFuzzyQuery, sname, sno, sjob);
    }

    @RequestMapping("/addStudent")
    @ResponseBody
    public Map<String, Object> addStudent(Student student) {
        Map<String, Object> resultmap = new HashMap<>();
        List<Map<String, Object>> stusno = studentService.findSno(student);
        if (stusno.size() > 0) {
            resultmap.put("code", 400);
            resultmap.put("message", ",学号重复，增加失败");
            return resultmap;
        } else {
            resultmap = studentService.addStudentInfo(student);
            return resultmap;
        }
    }

    @RequestMapping("/findSno")
    @ResponseBody
    public List<Map<String, Object>> findBySno(Student student) {
        List<Map<String, Object>> stu = studentService.findSno(student);
        return stu;
    }

    @RequestMapping("/updateStudent")
    @ResponseBody
    public Map<String, Object> updateStudent(Student student) {
        Map<String, Object> resultmap = new HashMap<>();
        List<Map<String, Object>> stu = studentService.findSno(student);
        int a = Integer.parseInt(stu.get(0).get("sid").toString());
        student.setSid(a);
        if (stu.size() > 0) {
            resultmap = studentService.updateStudent(student);
            resultmap.put("studentDialog", stu);
            return resultmap;
        } else {
            resultmap.put("code", 400);
            resultmap.put("message", "学号不存在，修改失败");
            return resultmap;
        }
    }

    //  删除功能代码
//  多个删除功能
    @RequestMapping("/deleteStudentbyids1")
    @ResponseBody
    public Map<String, Object> deleteManyStudent(String ids) {
        Map<String, Object> resultmap = new HashMap<>();
        System.out.println(ids);
        if (ids != null && ids.length() != 0) {
            String idsarry[] = ids.split(",");
            for (int i = 0; i < idsarry.length; i++) {
                System.out.println(idsarry[i]);
                studentService.deletebyids(idsarry[i]);
            }
            resultmap.put("code", 200);
            resultmap.put("message", "删除成功！");
        } else {
            resultmap.put("code", 400);
            resultmap.put("message", "删除失败");
        }
        return resultmap;
    }

    //    单个删除功能
    @RequestMapping("/deleteStudentbyids")
    @ResponseBody
    public Map<String, Object> deleteStudent(String ids) {
        Map<String, Object> resultmap = new HashMap<>();
        System.out.println(ids);
        if (ids != null && ids.length() != 0) {
            studentService.deletebyids(ids);
            resultmap.put("code", 200);
            resultmap.put("message", "删除成功！");
        } else {
            resultmap.put("code", 400);
            resultmap.put("message", "删除失败");
        }
        return resultmap;
    }
}
